package com.amor.ui

import android.annotation.SuppressLint
import android.os.SystemClock
import android.view.ViewGroup
import android.webkit.WebSettings
import androidx.webkit.ProxyConfig
import androidx.webkit.ProxyController
import androidx.webkit.WebViewFeature
import com.amor.widget.ExWebView
import com.hy.frame.util.LogUtil
import java.util.concurrent.Executor


class WebActivity : BaseWebActivity() {

    private val mWeb by lazy {
        ExWebView(this)
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun initView() {
        super.initView()
        initStartTime = SystemClock.elapsedRealtime()
        LogUtil.d(TAG, "初始化WebView start")
        initWebSettings(mWeb.settings, WebSettings.LOAD_NO_CACHE)
        initClient(mWeb)
        mBinding?.container?.addView(mWeb, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        LogUtil.d(TAG, "初始化WebView end time=%sms".format(SystemClock.elapsedRealtime() - initStartTime))
        showConsole("初始化WebView耗时=%sms\n".format(SystemClock.elapsedRealtime() - initStartTime))
    }

    override fun initData() {
        super.initData()
        setWebProxy()
        mWeb.loadUrl(URL)
    }

    private fun setWebProxy() {
        val proxy = ProxyConfig.Builder().addProxyRule("192.168.99.36:8899").addDirect().build()
        if(WebViewFeature.isFeatureSupported(WebViewFeature.PROXY_OVERRIDE)) {

            ProxyController.getInstance().setProxyOverride(proxy, {

            }, {

            })
        }

    }

    companion object {
        private const val TAG = "WebActivity"
        const val URL = "https://m.huize.com"
    }
}